package com.glsc.ngateway.opmanage.controller.outsys;

import com.glsc.ngateway.common.api.common.utils.PageUtil;
import com.glsc.ngateway.common.api.platform.resp.PlatformResponse;
import com.glsc.ngateway.common.base.domain.mysql.opmanagemain.outsys.OutSystemTemplateItem;
import com.glsc.ngateway.opmanage.dto.OutSystemAuthDto;
import com.glsc.ngateway.opmanage.dto.OutSystemQueryDto;
import com.glsc.ngateway.opmanage.service.outsys.OutSystemTemplateItemService;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;

/**
 * 外部系统权限模板关联表
 */
@RestController
@RequestMapping("/api/outSystem/template/item")
@Slf4j
public class OutSystemTemplateItemController {

    @Resource
    private OutSystemTemplateItemService templateItemService;


    /**
     * 分页查询
     *
     * @param outSystemQueryDto
     * @return
     */
    @PostMapping("/page")
    public PlatformResponse<Page<OutSystemTemplateItem>> findClientPage(@RequestBody OutSystemQueryDto outSystemQueryDto) {
        Map<String, Object> param = new HashMap<>();
        if (StringUtils.isNotBlank(outSystemQueryDto.getSystemName())) {
            param.put("LIKE_systemName", outSystemQueryDto.getSystemName());
        }

        Page<OutSystemTemplateItem> page = templateItemService.page(PageUtil.initPageNo(outSystemQueryDto.getPageNo()), PageUtil.initPageSize(outSystemQueryDto.getPageSize()), param);
        return PlatformResponse.successData(page);
    }

    /**
     * 根据客户id查询客户信息
     *
     * @param
     * @return
     */
    @GetMapping("/findById")
    public PlatformResponse<OutSystemTemplateItem> findById(@ApiParam(value = "id") @RequestParam(required = true) Integer id) {
        return PlatformResponse.successData(templateItemService.findById(id));
    }

    /**
     * 保存
     *
     * @param outSystemAuthInfo
     * @return
     */
    @PostMapping("/save")
    public PlatformResponse<OutSystemTemplateItem> save(@RequestBody OutSystemTemplateItem outSystemAuthInfo) {
        return PlatformResponse.successData(templateItemService.save(outSystemAuthInfo));
    }


    /**
     * 保存权限项配置
     *
     * @param dto
     * @return
     */
    @PostMapping("/saveAuthConfig")
    public PlatformResponse<?> saveAuthConfig(@RequestBody OutSystemAuthDto dto) {
        templateItemService.saveAuthConfig(dto);
        return PlatformResponse.success();
    }

    /**
     * 删除接口
     *
     * @return
     */
    @PostMapping("/delete")
    public PlatformResponse<?> delete(@RequestBody OutSystemTemplateItem outSystemAuthInfo) {
        templateItemService.deleteById(outSystemAuthInfo.getId());
        return PlatformResponse.success();
    }


}
